package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.TeachplanDto;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.TeachplanVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.mappers.TeachplanMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.system.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class TeachplanServiceImpl extends ServiceImpl<TeachplanMapper, Teachplan> implements TeachplanService {
    @Autowired
    TeachplanMapper teachplanMapper;
@Autowired
    CourseBaseService courseBaseService;
    @Override
    public ResponseResult treeNodes(Long courseId) {
        List<TeachplanVo> teachplanVos = teachplanMapper.treeNodes(courseId);
        //提取1级大纲
        TeachplanVo parent = null;
        for (TeachplanVo teachplanVo : teachplanVos) {
            if (teachplanVo.getGrade() == 1) {
                parent = teachplanVo;
            }
        } //提取2级大纲
        ArrayList<TeachplanVo> tows = new ArrayList<>();
        for (TeachplanVo teachplanVo : teachplanVos) {
            if (teachplanVo.getGrade() == 2) {
                tows.add(teachplanVo);
            }
        }
        //3级
        parent.setTeachPlanTreeNodes(tows);
        for (TeachplanVo tow : tows) {
            ArrayList<TeachplanVo> threes = new ArrayList<>();
            for (TeachplanVo teachplanVo : teachplanVos) {
                if (teachplanVo.getGrade() == 3 && teachplanVo.getParentid().equals(tow.getId())) {
                    threes.add(teachplanVo);
                }
            }
            tow.setTeachPlanTreeNodes(threes);
        }
        return  ResponseResult.okResult(parent);
    }

    @Override
    public ResponseResult saveOrUpdatePlan(TeachplanDto dto) {
        Long courseId = dto.getCourseId();
        // 查询课程
        CourseBase courseBase = courseBaseService.getById(courseId);
        //判断状态
        if(! courseBase.getAuditStatus().equals(CourseConstants.AUDIT_DISPAST_STATUS)
                && ! courseBase.getAuditStatus().equals(CourseConstants.AUDIT_UNPAST_STATUS)) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        //处理Parentid为空的情况
        if(dto.getParentid() == null) {
            //保存2级课程计划，查询1级课程计划
            LambdaQueryWrapper<Teachplan> qw = new LambdaQueryWrapper<>();
            qw.eq(Teachplan::getGrade,CourseConstants.FIRST_LEVEL);
            qw.eq(Teachplan::getCourseId,courseId);
            Teachplan parent = this.getOne(qw);
            //判断parent是否为空
            dto.setParentid(parent.getId());
        }
        //将dto转化成java实体类
        Teachplan teachplan = BeanHelper.copyProperties(dto, Teachplan.class);
        //判断保存的课程计划的ID
        Boolean flag = false;
        if(teachplan.getId() == null) {


            flag = this.save(teachplan);
        }else {
            //ID存在，更新
            flag = this.updateById(teachplan);
        }

        if(!flag) {
            throw new BusinessException(ErrorCode.ERROR);
        }

        return ResponseResult.okResult();
    }
}

